import { defineStore } from "pinia";
import type { LocaleSetting, LocaleType } from "/#/config";
import { createLocaleStorage } from "/@/utils/cache";
import { LOCALE_KEY } from "/@/enums/cacheEnum";
import { localeSetting } from "/@/settings/localeSettings";
import { store } from '/@/store';

const ls = createLocaleStorage();

const lsLocaleSetting = (ls.get(LOCALE_KEY) || localeSetting) as LocaleSetting;

interface LocaleState {
    localInfo: LocaleSetting;

    pathTitleMap: object | any;

    appIndexTheme: string;

    appMainPth: string
}

export const useLocaleStore = defineStore('app-locale',
    {
    state: (): LocaleState => {
        return {localInfo: lsLocaleSetting,
        pathTitleMap: {},
        appIndexTheme: '',
        appMainPth: ''}
    },
    getters: {
        getShowPicker(): boolean {
            return !!this.localInfo?.showPicker;
        },
        getLocale(): LocaleType {
            return this.localInfo?.locale ?? 'zh_CN';
        },
        getPathTitle: (state) => {
            return (path: string) => state.pathTitleMap[path];
        },
        getAppIndexTheme(): string {
            return this.appIndexTheme;
        },
        getAppMainPth(): string {
            return this.appMainPth;
        },

    },
    actions: {
        setLocaleInfo(info: Partial<LocaleSetting>) {
            this.localInfo = { ...this.localInfo, ...info};
            ls.set(LOCALE_KEY, this.localInfo);
        },
        initLocale() {
            this.setLocaleInfo({
                ...localeSetting,
                ...this.localInfo,
            });
        },
        setPathTitle(path: string, title: any){
            this.pathTitleMap[path] = title;
        },
        setAppIndexTheme(theme: string){
            this.appIndexTheme = theme;
        },
        setAppMainPth(path: string){
            this.appMainPth = path;
        }
    },
});



export function userLocaleStoreWithOut(){
    return useLocaleStore(store);
}
